package StudyCode.summary.zipfile;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class UncompressingFiles {
	public static void main(String[] args) throws Exception {
		ZipFile file = new ZipFile("E:/Copy/1.zip");
		FileSystem fileSystem = FileSystems.getDefault();
		Enumeration<? extends ZipEntry> entries = file.entries();
		String uncompressedDirectory = "uncompressed/";
		Files.createDirectory(fileSystem.getPath(uncompressedDirectory));
		while (entries.hasMoreElements()) {
			ZipEntry entry = entries.nextElement();
			if (entry.isDirectory()) {
				System.out.println("创建目录:" + uncompressedDirectory + entry.getName());
				Files.createDirectories(fileSystem.getPath(uncompressedDirectory
						+ entry.getName()));
			} else {
				InputStream is = file.getInputStream(entry);
				System.out.println("文件 :" + entry.getName());
				BufferedInputStream bis = new BufferedInputStream(is);

				String uncompressedFileName = uncompressedDirectory + entry.getName();
				Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
				Files.createFile(uncompressedFilePath);
				try (FileOutputStream fileOutput = new FileOutputStream(
						uncompressedFileName)) {
					while (bis.available() > 0) {
						fileOutput.write(bis.read());
					}
				}
				System.out.println("写入 :" + entry.getName());
			}
		}
	}
}
